<?php
class RM_GracePriceBands extends RM_Model_Multilingual {
    protected $_name = 'rm_grace_price_bands';
    protected $_rowClass = 'RM_GracePriceBands_Row';

    /**
     * We extends supper to change type of a column, from varchar to text
     * @param string $iso
     */
    function addLanguage($iso)
    {
        $adapter = $this->getAdapter();
        $sql = "ALTER TABLE `".$this->_name."` ADD `".$iso."` TEXT NOT NULL ";
        $adapter->query($sql);
    }
    /**
     * Return template by it's id
     *
     * @param string $id
     * @return RM_GracePriceBands_Row
     */
    function fetchByID($id){
        return $this->fetchRow($this->select()->where('id=?', $id));
    }
    /**
     * Gets the identifier of the band containing a given number of days for a given unit
     * @param [int] $days the number of days
     * @return the requested indentifier, or <code>-1<code> if not found
     */
    public function findContaining($days) {
        $bandId = -1;
        try {
            $sel = $this->select();
            $sel->where("start_days<=?", $days)
                ->where("end_days>=?", $days);
            $sel->limit(1, 0);
            $res = $this->fetchRow($sel);
            if (!empty($res)) {
                $bandId = $res->id;
            }
        }
        catch(Exception $e) {
            RM_Log::toLog("failed to get band containing $days days for unit $unitId", $e);
        }
        return $bandId;
    }
}
